package com.example.flightsearch.data

import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.map
import javax.inject.Inject

class OfflineFavoriteRepository @Inject constructor(private val favoriteDao: FavoriteDao) : FavoriteRepository {
    override fun have(favorite: Favorite): Flow<Boolean> =
        favoriteDao.countByDepartureCodeAndDestinationCode(favorite.departureCode, favorite.destinationCode)
            .map { it > 0 }

    override suspend fun delete(favorite: Favorite) =
        favoriteDao.deleteByDepartureCodeAndDestinationCode(
            FavoriteDepartureCodeAndDestinationCode(
                departureCode = favorite.departureCode,
                destinationCode = favorite.destinationCode
            )
        )

    override suspend fun add(favorite: Favorite) =
        favoriteDao.insert(favorite)
}
